package com.wyth.lottery.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wyth.lottery.R;
import com.wyth.lottery.base.BaseActivity;
import com.wyth.lottery.base.SaveUtils;
import com.wyth.lottery.fragment.BZFragment;
import com.wyth.lottery.fragment.DTFragment;
import com.wyth.lottery.service.Entity.Detail;
import com.wyth.lottery.service.Entity.SSQSelectBean;
import com.wyth.lottery.service.presenter.DetailPresenter;
import com.wyth.lottery.service.view.DetailView;
import com.wyth.lottery.utils.BuildParamUtils;
import com.wyth.lottery.utils.MyApplication;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by phh on 2017/10/31.
 * 双色球的选号界面
 */

public class SSQSelectActivity extends BaseActivity {
    @BindView(R.id.tv_number)
    TextView mTvNumber;
    private DetailPresenter mDetailPresenter;
    private String detailInfo;


   public Handler mHandler = new Handler();

   public Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            mDetailPresenter.getDetail(detailInfo);
        }
    };

    boolean bzboolean = false;
    boolean dtboolean = false;
    @BindView(R.id.zw_bz_title)
    TextView mZwBzTitle;
    @BindView(R.id.zw_dt_title)
    TextView mZwDtTitle;
    @BindView(R.id.relativelayout)
    RelativeLayout mRelativelayout;
    @BindView(R.id.iv_bz_img)
    ImageView mIvBzImg;
    @BindView(R.id.iv_dt_img)
    ImageView mIvDtImg;
    public SSQSelectBean.IssueListBean.IssueBean mIssueBean;
    private BZFragment mBzFragment;


    @Override
    protected int getContentID() {
        return R.layout.activity_ssq_select;
    }

    @Override
    protected void initView() {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.framelayout, new BZFragment())
                .commit();
        mIvBzImg.setVisibility(View.VISIBLE);
        mIvDtImg.setVisibility(View.INVISIBLE);


    }

    @Override
    protected void initDate() {
        EventBus.getDefault().register(this);
        String stickyEvent = EventBus.getDefault().getStickyEvent(String.class);
        if (stickyEvent != null) {
            EventBus.getDefault().removeStickyEvent(stickyEvent);
        }
        initData();
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Object event) {
        if (event instanceof String) {
            if (event.equals("DT")) {
                bzboolean = false;
                mZwDtTitle.setTextSize(px2dip(MyApplication.getContext(), 60));
                mZwBzTitle.setTextSize(px2dip(MyApplication.getContext(), 44));
                mZwBzTitle.setEnabled(true);
                mZwDtTitle.setEnabled(false);
                mIvBzImg.setVisibility(View.INVISIBLE);
                mIvDtImg.setVisibility(View.VISIBLE);
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.framelayout, new DTFragment())
                        .commit();
            }
        }

    }


    @OnClick({R.id.zw_bz_title, R.id.zw_dt_title})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.zw_bz_title:
                bzboolean = !bzboolean;
                if (bzboolean) {
                    dtboolean = false;
                    mZwBzTitle.setTextSize(px2dip(MyApplication.getContext(), 60));
                    mZwDtTitle.setTextSize(px2dip(MyApplication.getContext(), 44));
                    mZwBzTitle.setEnabled(false);
                    mZwDtTitle.setEnabled(true);
                    mIvBzImg.setVisibility(View.VISIBLE);
                    mIvDtImg.setVisibility(View.INVISIBLE);
                    mBzFragment = new BZFragment();
                    getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.framelayout, mBzFragment)
                            .commit();

                } else {
                    mZwBzTitle.setEnabled(true);
                    mZwDtTitle.setEnabled(false);
                    mIvBzImg.setVisibility(View.INVISIBLE);
                    mIvDtImg.setVisibility(View.VISIBLE);
                    dtboolean = true;
                    mZwBzTitle.setTextSize(px2dip(MyApplication.getContext(), 44));
                    mZwDtTitle.setTextSize(px2dip(MyApplication.getContext(), 60));
                    getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.framelayout, new DTFragment())
                            .commit();
                }

                break;
            case R.id.zw_dt_title:
                dtboolean = !dtboolean;
                if (dtboolean) {
                    bzboolean = false;
                    mZwDtTitle.setTextSize(px2dip(MyApplication.getContext(), 60));
                    mZwBzTitle.setTextSize(px2dip(MyApplication.getContext(), 44));
                    mZwBzTitle.setEnabled(true);
                    mZwDtTitle.setEnabled(false);
                    mIvBzImg.setVisibility(View.INVISIBLE);
                    mIvDtImg.setVisibility(View.VISIBLE);
                    getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.framelayout, new DTFragment())
                            .commit();
                } else {
                    mIvDtImg.setVisibility(View.INVISIBLE);
                    mIvDtImg.setVisibility(View.VISIBLE);
                    bzboolean = true;
                    mZwBzTitle.setTextSize(px2dip(MyApplication.getContext(), 60));
                    mZwDtTitle.setTextSize(px2dip(MyApplication.getContext(), 44));
                    mZwBzTitle.setEnabled(false);
                    mZwDtTitle.setEnabled(true);
                    getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.framelayout, new BZFragment())
                            .commit();
                }
                break;
        }
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDetailPresenter.onStop();
        EventBus.getDefault().unregister(this);
        mHandler.removeCallbacks(mRunnable);
    }

    private void initData() {
        mDetailPresenter = new DetailPresenter(this);
        Gson gson = new Gson();
        Map<String, String> param = new HashMap<String, String>();
        param.put("sid", "yhl");
        param.put("cmd", "3301");
        param.put("platform", "andorid");
        param.put("sysVer", "android4.2");
        param.put("softVer", "v1.0");
        param.put("width", "800");
        param.put("height", "1024");
        param.put("machId", "aaaaa");
        param.put("func", "cur");


        String msgs = "{\"lotteryId\":\"001\"}";
        param.put("msg", msgs);
        String currMd5 = BuildParamUtils.buildMd5(param, "yhlkey");
        param.put("md5", currMd5);
        detailInfo = gson.toJson(param);

        mHandler.post(mRunnable);
        mDetailPresenter.onCreate();
        mDetailPresenter.attachView(mBookVieww);
    }

    private DetailView mBookVieww = new DetailView() {

        @Override
        public void onSuccess(Detail detail) {
            Gson gson = new Gson();
            SSQSelectBean ssqSelectBean = gson.fromJson(detail.getResult(), SSQSelectBean.class);
            List<SSQSelectBean.IssueListBean> issueList = ssqSelectBean.getIssueList();
            SSQSelectBean.IssueListBean issueListBean = issueList.get(0);
            List<SSQSelectBean.IssueListBean.IssueBean> issue = issueListBean.getIssue();
            mIssueBean = issue.get(0);
            mTvNumber.setText(mIssueBean.getName());

        }

        @Override
        public void onError(String result) {

        }
    };


}
